package com.dh.web.controller;


import com.dh.core.security.RoleConstant;
import com.dh.core.util.BeanValueUtil;
import com.dh.entity.User;
import com.dh.entity.UserRP;
import com.dh.entity.vo.UserRPVO;
import com.dh.service.UserRPService;
import com.dh.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/user-rp")
@Controller
public class UserRPController {

    @Autowired
    private UserRPService rpService;


    @Autowired
    private UserService userService;


    /**
     * 转到列表展示页
     *
     * @param model
     * @param page
     * @param nickname
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresUser
    public String show(Model model, @RequestParam(required = false, value = "page", defaultValue = "1") int page, String nickname) {
        Page<UserRPVO> pagination =
            rpService.page(new PageRequest(page - 1, 100, Sort.Direction.ASC, "id"));
        model.addAttribute("pagination", pagination);
        return "admin/user-rp/rp_list";
    }

    /**
     * 转到添加页面
     *
     * @return
     */
    @RequestMapping("/new")
    @RequiresUser
    public String editNew(Model model) {
        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-rp/rp_create";
    }



    /**
     * 添加党员奖惩
     *
     * @param rp
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @RequiresUser
    public String create(UserRP rp, RedirectAttributes redirectAttributes) {

        try {
            rp.setStatus(0);//登记中
            rpService.create(rp);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "出现异常");
            return "redirect:/admin/user-rp/new";
        }
        return "redirect:/admin/user-rp/list";
    }

    /**
     * 删除一个用户奖惩
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<>();
        rpService.delete(id);
        map.put("success", true);
        return map;
    }


    /**
     * 转到编辑奖惩页
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    @RequiresUser
    public String edit(@PathVariable Long id, Model model) {
        UserRP user = rpService.get(id);
        model.addAttribute("u", user);
        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-rp/rp_update";
    }

    /**
     * 转到审批页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/approval/{id}", method = RequestMethod.GET)
    @RequiresUser
    public String toApproval(@PathVariable Long id, Model model) {
        UserRP user = rpService.get(id);
        model.addAttribute("u", user);
        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-rp/rp_approval";
    }

    /**
     * 审批奖惩信息
     *
     * @param id
     * @param approvalDept
     * @param approvalOpinion
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/approval/{id}", method = RequestMethod.POST)
    @RequiresUser
    public String approval(@PathVariable("id") long id, @Param("approvalDept") String approvalDept, @Param("approvalOpinion") String approvalOpinion,@Param("approvalTime") String approvalTime, RedirectAttributes redirectAttributes) {
        try {
            UserRP rp = rpService.get(id);
            rp.setApprovalDept(approvalDept);
            rp.setApprovalOpinion(approvalOpinion);
            rp.setApprovalTime(approvalTime);
            rp.setStatus(1);//审批完成
            rpService.update(rp);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "出现错误");
            return "redirect:/admin/user-rp/edit/" + id;
        }
        return "redirect:/admin/user-rp/list";
    }



    /**
     * 更新用户奖惩
     *
     * @param id
     * @param rp
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public String update(@PathVariable("id") long id, UserRP rp, RedirectAttributes redirectAttributes) {

        try {
            UserRP p = rpService.get(id);
            rp.setStatus(p.getStatus());
            BeanValueUtil.copyProperties(rp,p);
            p.setLastModifyTime(new Date());
            rpService.update(p);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "出现错误");
            return "redirect:/admin/user-rp/edit/" + id;
        }
        return "redirect:/admin/user-rp/list";
    }



}
